//
//  LDSSectionBaseView.m
//  LDSSmartHome
//
//  Created by kai_leedarson on 2017/11/8.
//  Copyright © 2017年 leedarson. All rights reserved.
//

#import "LDSSectionBaseView.h"
#import "LDSSecionBaseModel.h"
#import "UIImage+DisclosureIndicator.h"
#import "Masonry.h"

@interface LDSSectionBaseView ()
@property(nonatomic, strong) UIImageView *rightView;
@end
@implementation LDSSectionBaseView
- (UIImageView *)rightView {
    if (_rightView == nil) {
        UIImage *image = [UIImage DisclosureIndicatorImage];
        _rightView = [[UIImageView alloc] initWithImage:image];
        CGSize size = CGSizeMake(7.5,12.5);
        _rightView.frame = CGRectMake([UIScreen mainScreen].bounds.size.width - size.width - 20, (self.frame.size.height - size.height)/2, size.width,size.height);
    }
    return _rightView;
}

- (UIImageView *)bottomlineView {
    if (_bottomlineView == nil) {
        UIImage *image = [UIImage imageNamed:@"parting line"];
        _bottomlineView = [[UIImageView alloc] initWithImage:image];
        _bottomlineView.frame = CGRectMake(0 ,self.frame.size.height -1,[UIScreen mainScreen].bounds.size.width, 1);
    }
    return _bottomlineView;
}

- (UIImageView *)addView {
    if (_addView == nil) {
        UIImage *image = [UIImage imageNamed:@"加号"];
        _addView = [[UIImageView alloc] initWithImage:image];
        _addView.frame = CGRectMake([UIScreen mainScreen].bounds.size.width - image.size.width - 16, (self.frame.size.height - image.size.height)/2, image.size.width,image.size.height);
    }
    return _addView;
}

- (UISwitch *)rightSwitch {
    if (_rightSwitch == nil) {
        CGSize size = CGSizeMake(51,27);
        CGRect frame = CGRectMake([UIScreen mainScreen].bounds.size.width - size.width - 16, (self.frame.size.height - size.height)/2, size.width,size.height);
        _rightSwitch = [[UISwitch alloc] initWithFrame:frame];
    }
    return _rightSwitch;
}

- (UILabel *)titleName {
    if (_titleName == nil) {
        _titleName = [[UILabel alloc] init];
        _titleName.textColor = [UIColor lightGrayColor];
        _titleName.textAlignment = NSTextAlignmentLeft;
    }
    return _titleName;
}

- (UILabel *)detailText {
    if (_detailText == nil) {
        _detailText = [[UILabel alloc] init];
        _detailText.textColor = [UIColor lightGrayColor];
        _detailText.textAlignment = NSTextAlignmentRight;
    }
    return _detailText;
}

- (void)setModel:(id)model {
    [super setModel:model];
    if ([model isKindOfClass:[LDSSecionBaseModel class]] ) {
        LDSSecionBaseModel *baseModel = (LDSSecionBaseModel *)model;
        self.titleName.text = baseModel.text;
        self.detailText.text = baseModel.rightDetail;
        if (baseModel.status == LDSSectionModelRightStatusAdd) {
            self.addView.hidden = NO;
            self.rightView.hidden = YES;
            self.rightSwitch.hidden = YES;
        } else if (baseModel.status == LDSSectionModelRightStatusIndicator) {
            self.addView.hidden = YES;
            self.rightView.hidden = NO;
            self.rightSwitch.hidden = YES;
        } else if (baseModel.status == LDSSectionModelRightStatusSwitch) {
            self.addView.hidden = YES;
            self.rightView.hidden = YES;
            self.rightSwitch.hidden = NO;
        } else {
            self.addView.hidden = YES;
            self.rightView.hidden = YES;
            self.rightSwitch.hidden = YES;
        }
    }
}

- (void)viewSetup {
    [self addSubview:self.rightView];
    [self addSubview:self.addView];
    [self addSubview:self.titleName];
    [self addSubview:self.detailText];
    [self addSubview:self.rightSwitch];
    [self addSubview:self.bottomlineView];
}


- (void)frameSetup {
    [_titleName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.left.equalTo(self).offset(16);
    }];
    [_detailText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.right.equalTo(@-40);
    }];
    
}



@end
